package com.study.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.study.user.dao.UserDao;
import com.study.user.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.utils.PageUtils;
import com.study.common.utils.Query;

import com.study.user.dao.DepartmentDao;
import com.study.user.entity.DepartmentEntity;
import com.study.user.service.DepartmentService;
import org.springframework.transaction.annotation.Transactional;


@Service("departmentService")
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, DepartmentEntity> implements DepartmentService {

    @Autowired
   private UserDao userDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<DepartmentEntity> page = this.page(
//                new Query<DepartmentEntity>().getPage(params),
//                new QueryWrapper<DepartmentEntity>()
//        );
//
//        return new PageUtils(page);
        return null;
    }

    @Override
    public List<DepartmentEntity> listTree() {
        //查询所有的部门
       List<DepartmentEntity> alldpm= baseMapper.selectList(null);
       //过滤一级部门
       alldpm.stream().filter(departmentEntity -> departmentEntity.getParentId()==0).map(meu->{
           //获取 子部门
                    meu.setChildrens(getChildrens(meu,alldpm));
                   return meu;
               }
               ).collect(Collectors.toList());

        return alldpm;
    }
    /**
     * 递归 将部门 展示成树形
     * @param root
     * @param all
     * @return
     */
    private List<DepartmentEntity> getChildrens(DepartmentEntity root, List<DepartmentEntity> all){

        List<DepartmentEntity> childrens= all.stream().filter(departmentEntity->
                departmentEntity.getParentId() == root.getId()
        ).map(departmentEntity ->{
            //1、找到子部门
            departmentEntity.setChildrens(getChildrens(departmentEntity,all));
            return departmentEntity;
        }).collect (Collectors.toList());

        return childrens;
    }

    @Override
    @Transactional
    public Set<Integer>  deleteTree(int id) {
        List<DepartmentEntity> list=baseMapper.selectList(null);
        Set<Integer> delIds=new HashSet<>();
        delIds.add(id);
        getIds(id,list,delIds);
        //递归查找出要删除的delIds
        if(!delIds.isEmpty()){
            QueryWrapper<DepartmentEntity> queryWrapper=new QueryWrapper<>();
            queryWrapper.in("id",delIds);
            baseMapper.update(DepartmentEntity.builder().status("1").build(),queryWrapper);
            //再删除部门下的员工
            QueryWrapper<UserEntity> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.in("dpm_Id",delIds);
            userDao.update(UserEntity.builder().status("1").build(),userQueryWrapper);
        }
        return delIds;
    }

    /**
     * 递归 查找出子部门
     * @param id
     * @param all
     * @param delIds
     */
    private void getIds(Integer id, List<DepartmentEntity> all, Set<Integer> delIds){
       for(DepartmentEntity departmentEntity:all){
           if(id.equals(departmentEntity.getParentId())){
               //将自己的子部门 添加到删除集合
               delIds.add(departmentEntity.getId());
               //再将子部门规定查找子部门
               getIds(departmentEntity.getId(),all,delIds);
           }
        }
    }



}